package apiTest;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.UUID;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import beans.Person;
import beans.WebPage;

/**
 * 实现增加索引
 * 
 * @author kfh
 *
 */
public class CRUDTest {
	public static void createIndex(Object o, String indexName, String typeName, String id) {
		// on startup
		Client client = null;
		try {
			client = TransportClient.builder().build()
					.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String json = ESUtils.toJson(o);
		// 在这里创建我们要索引的对象
		IndexResponse response = client.prepareIndex(indexName, typeName)
				// 必须为对象单独指定ID
				.setId(id).setSource(json).execute().actionGet();
		// 多次index这个版本号会变
		System.out.println("response.version():" + response.getVersion());

		// on shutdown
		client.close();

	}
	
	public static void main(String[] args) {
		
		
//		createIndex(o, "test", "test", "");
	}
}
